﻿using System; 
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Matrix.Common.DBUtility;
using Matrix.MaKaYunShang.Model;

namespace Matrix.MaKaYunShang.DAL
{
	public partial class OrderDetailDAL : BaseDAL<OrderDetailDAL>
	{
        #region 默认方法
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(OrderDetail model)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into OrderDetail(");
            strSql.Append("OrderId,ShopCommodityId,Price,Quantity,CommodityName,CommodityBrand,CommodityCateName,CommoditySpec,CommodityUnit,CommodityLogo,CommoditySuggestedPrice,CommodityBarCode,IsGift,CommodityGiftId,ShopCommission,AddTime,IsDelete");
            strSql.Append(") values (");
            strSql.Append("@OrderId,@ShopCommodityId,@Price,@Quantity,@CommodityName,@CommodityBrand,@CommodityCateName,@CommoditySpec,@CommodityUnit,@CommodityLogo,@CommoditySuggestedPrice,@CommodityBarCode,@IsGift,@CommodityGiftId,@ShopCommission,@AddTime,@IsDelete");
            strSql.Append(") ");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
			            new SqlParameter("@OrderId", SqlDbType.Int,4) ,            
                        new SqlParameter("@ShopCommodityId", SqlDbType.Int,4) ,            
                        new SqlParameter("@Price", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@Quantity", SqlDbType.Int,4) ,            
                        new SqlParameter("@CommodityName", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@CommodityBrand", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@CommodityCateName", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@CommoditySpec", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@CommodityUnit", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@CommodityLogo", SqlDbType.NVarChar,300) ,            
                        new SqlParameter("@CommoditySuggestedPrice", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@CommodityBarCode", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@IsGift", SqlDbType.Bit,1) ,            
                        new SqlParameter("@CommodityGiftId", SqlDbType.Int,4) ,            
                        new SqlParameter("@ShopCommission", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@AddTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@IsDelete", SqlDbType.Bit,1)             
              
            };

            parameters[0].Value = model.OrderId;
            parameters[1].Value = model.ShopCommodityId;
            parameters[2].Value = model.Price;
            parameters[3].Value = model.Quantity;
            parameters[4].Value = model.CommodityName;
            parameters[5].Value = model.CommodityBrand;
            parameters[6].Value = model.CommodityCateName;
            parameters[7].Value = model.CommoditySpec;
            parameters[8].Value = model.CommodityUnit;
            parameters[9].Value = model.CommodityLogo;
            parameters[10].Value = model.CommoditySuggestedPrice;
            parameters[11].Value = model.CommodityBarCode;
            parameters[12].Value = model.IsGift;
            parameters[13].Value = model.CommodityGiftId;
            parameters[14].Value = model.ShopCommission;
            parameters[15].Value = model.AddTime;
            parameters[16].Value = model.IsDelete; var obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            return obj == null ? 0 : Convert.ToInt32(obj);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(OrderDetail model)
        {
            var strSql = new StringBuilder();
            strSql.Append("update OrderDetail set ");

            strSql.Append(" OrderId = @OrderId , ");
            strSql.Append(" ShopCommodityId = @ShopCommodityId , ");
            strSql.Append(" Price = @Price , ");
            strSql.Append(" Quantity = @Quantity , ");
            strSql.Append(" CommodityName = @CommodityName , ");
            strSql.Append(" CommodityBrand = @CommodityBrand , ");
            strSql.Append(" CommodityCateName = @CommodityCateName , ");
            strSql.Append(" CommoditySpec = @CommoditySpec , ");
            strSql.Append(" CommodityUnit = @CommodityUnit , ");
            strSql.Append(" CommodityLogo = @CommodityLogo , ");
            strSql.Append(" CommoditySuggestedPrice = @CommoditySuggestedPrice , ");
            strSql.Append(" CommodityBarCode = @CommodityBarCode , ");
            strSql.Append(" IsGift = @IsGift , ");
            strSql.Append(" CommodityGiftId = @CommodityGiftId , ");
            strSql.Append(" ShopCommission = @ShopCommission , ");
            strSql.Append(" AddTime = @AddTime , ");
            strSql.Append(" IsDelete = @IsDelete  ");
            strSql.Append(" where Id=@Id ");

            SqlParameter[] parameters = {
			            new SqlParameter("@Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@OrderId", SqlDbType.Int,4) ,            
                        new SqlParameter("@ShopCommodityId", SqlDbType.Int,4) ,            
                        new SqlParameter("@Price", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@Quantity", SqlDbType.Int,4) ,            
                        new SqlParameter("@CommodityName", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@CommodityBrand", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@CommodityCateName", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@CommoditySpec", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@CommodityUnit", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@CommodityLogo", SqlDbType.NVarChar,300) ,            
                        new SqlParameter("@CommoditySuggestedPrice", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@CommodityBarCode", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@IsGift", SqlDbType.Bit,1) ,            
                        new SqlParameter("@CommodityGiftId", SqlDbType.Int,4) ,            
                        new SqlParameter("@ShopCommission", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@AddTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@IsDelete", SqlDbType.Bit,1)             
              
            };

            parameters[0].Value = model.Id;
            parameters[1].Value = model.OrderId;
            parameters[2].Value = model.ShopCommodityId;
            parameters[3].Value = model.Price;
            parameters[4].Value = model.Quantity;
            parameters[5].Value = model.CommodityName;
            parameters[6].Value = model.CommodityBrand;
            parameters[7].Value = model.CommodityCateName;
            parameters[8].Value = model.CommoditySpec;
            parameters[9].Value = model.CommodityUnit;
            parameters[10].Value = model.CommodityLogo;
            parameters[11].Value = model.CommoditySuggestedPrice;
            parameters[12].Value = model.CommodityBarCode;
            parameters[13].Value = model.IsGift;
            parameters[14].Value = model.CommodityGiftId;
            parameters[15].Value = model.ShopCommission;
            parameters[16].Value = model.AddTime;
            parameters[17].Value = model.IsDelete; return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {
            var strSql = new StringBuilder();
            strSql.Append("update OrderDetail set IsDelete=1 ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 批量删除一批数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            var strSql = new StringBuilder();
            strSql.Append("update OrderDetail set IsDelete=1 ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            return DbHelperSQL.ExecuteSql(strSql.ToString()) > 0;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DataSet GetModel(int Id)
        {
            var strSql = new StringBuilder();
            strSql.Append("select Id, OrderId, ShopCommodityId, Price, Quantity, CommodityName, CommodityBrand, CommodityCateName, CommoditySpec, CommodityUnit, CommodityLogo, CommoditySuggestedPrice, CommodityBarCode, IsGift, CommodityGiftId, ShopCommission, AddTime, IsDelete  ");
            strSql.Append("  from OrderDetail ");
            strSql.Append(" where Id=@Id  and IsDelete=0");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            return ds.Tables[0].Rows.Count > 0 ? ds : null;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM OrderDetail where IsDelete=0 ");
            if (!string.IsNullOrEmpty(strWhere))
            {
                strSql.Append(" and " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        #endregion
		
		#region 扩展方法
        /// <summary>
        /// 通过ORDERID获取订单详情列表
        /// </summary>
        public List<OrderDetail> GetOrderDetailListByOrderId(int orderId)
        {
            var strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM OrderDetail where OrderId=@OrderId AND IsDelete=0 ");
            SqlParameter[] parameters = {
					new SqlParameter("@OrderId", SqlDbType.Int,4)
			};
            parameters[0].Value = orderId;
            var lsRet = DbHelperSQL.ExecuteSqlList<OrderDetail>(strSql.ToString(), parameters);
            return lsRet == null || lsRet.Count == 0 ? null : lsRet.ToList();
        }

        /// <summary>
        /// 根据时间段获取用户已获得的赠品数量
        /// </summary>
        /// <param name="consumerId">用户ID</param>
        /// <param name="commodityGiftId">赠品ID</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <returns>返回时间段内用户已获得的赠品数量</returns>
        /// <remarks>2016-09-22 万乐园 创建</remarks>
        public int GetOrderGiftCountByTime(int consumerId, int commodityGiftId, string startTime, string endTime)
        {
            var sql = new StringBuilder();
            var parameters = new List<SqlParameter>();
            sql.Append(" select COUNT(*) as num from OrderDetail od");
            sql.Append(" left join orders os on od.OrderId=os.Id");
            sql.Append(" where os.IsDelete=0 and od.IsDelete=0 and os.OrderStatus!=5 and os.OrderStatus!=6");
            sql.Append(" and os.ConsumerId=@ConsumerId");
            parameters.Add(new SqlParameter { ParameterName = "@ConsumerId", Value = consumerId });
            sql.Append(" and od.CommodityGiftId=@CommodityGiftId");
            parameters.Add(new SqlParameter { ParameterName = "@CommodityGiftId", Value = commodityGiftId });
            if (!string.IsNullOrEmpty(startTime))
            {
                sql.Append(" and os.OrderTime>=@StartTime");
                parameters.Add(new SqlParameter { ParameterName = "@StartTime", Value = startTime });
            }
            if (!string.IsNullOrEmpty(endTime))
            {
                sql.Append(" and os.OrderTime<=@EndTime");
                parameters.Add(new SqlParameter { ParameterName = "@EndTime", Value = endTime });
            }
            return int.Parse(DbHelperSQL.GetSingle(sql.ToString(), parameters.ToArray()).ToString());
        }
		#endregion
	}
}
